﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PublicData
{
    /// <summary>
    /// 功能扩展
    /// </summary>
    public static class QueryableExtension
    {
        /// <summary>
        /// IQueryable分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static IQueryable<T> Paged<T>(this IQueryable<T> query, int currentPage = 1, int pageSize = 20)
        {
            if (currentPage < 1)
            {
                currentPage = 1;
            }
            query = query.Skip((currentPage - 1) * pageSize).Take(pageSize);
            return query;
        }

        /// <summary>
        /// List分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static List<T> ListPaged<T>(this List<T> query, int currentPage = 1, int pageSize = 20)
        {
            if (currentPage < 1)
            {
                currentPage = 1;
            }
            query = query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
            return query;
        }

        /// <summary>
        /// List分页
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="query"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public static List<T> ListPaged<T>(this List<T> query, out int totalCount, int currentPage = 1, int pageSize = 20)
        {
            if (currentPage < 1)
                currentPage = 1;

            totalCount = query.Count;
            query = query.Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();


            return query;
        }
    }
}
